package com.yanghui.lingyue.functions.defaultFunction

import com.icedata.sika.bridge.SikaEvent
import com.icedata.sika.bridge.SikaGroupEvent
import com.icedata.sika.bridge.SikaUserEvent
import com.icedata.sika.core.PreProcessor
import com.icedata.sika.core.Sika
import com.icedata.sika.util.Logger
import com.yanghui.lingyue.bot.Bot
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

object BotPreprocessor {

    fun preprocess(sikaEvent: SikaEvent, sika: Sika): PreProcessor.Result {
        if (sikaEvent.message.contentToString().contains("现充")
            && sikaEvent.sender.id != 2411046022L
        ) {
            sikaEvent as SikaGroupEvent
            sika.delay(1000) {
                GlobalScope.launch {
                    try {
                        sikaEvent.sendGroup.recallMessage(sikaEvent.message)
                    } catch (e: Exception) {
                        Logger.logError(e)
                    }
                }
            }
        }
        if (sikaEvent is SikaUserEvent)
            return PreProcessor.Result.BLOCK
        sikaEvent as SikaGroupEvent
        if (sikaEvent.sendGroup.id !in Bot.groupList.keys)
            return PreProcessor.Result.BLOCK
        if (Bot.userList[sikaEvent.sender.id]?.isForbidden == true)
            return PreProcessor.Result.BLOCK
        return PreProcessor.Result.ADOPT
    }

}